package cn.hebutrun.oulabot.service.impl;

import cn.hebutrun.oulabot.common.Constants;
import cn.hebutrun.oulabot.common.util.ApiUtil;
import cn.hebutrun.oulabot.common.util.BotUtil;
import cn.hebutrun.oulabot.entity.enums.MessageTypeEnum;
import cn.hebutrun.oulabot.entity.response.BotMessageResponse;
import cn.hebutrun.oulabot.quartz.QuartzUtil;
import cn.hebutrun.oulabot.quartz.job.RemindWeiboCheckinJob;
import cn.hebutrun.oulabot.service.*;
import lombok.RequiredArgsConstructor;
import org.quartz.*;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author zhaolong
 * @since 2022-09-28
 */
@Service
@RequiredArgsConstructor
public class MessageServiceImpl implements IMessageService {

  private final ITodoService iTodoService;
  private final ILunchInfoService iLunchInfoService;
  private final IPicService iPicService;
  private final ApiUtil apiUtil;
  private final QuartzUtil quartzUtil;
  private final IBotService iBotService;

  @Override
  public void onOrder(BotMessageResponse botMessageResponse) throws SchedulerException {
    String rawMessage = botMessageResponse.getRawMessage();
    String order = rawMessage.replaceAll("#", "").replaceAll("＃", "").trim();
    switch (order) {
      case "风鹰侠" -> {
        if (MessageTypeEnum.GROUP.getType().equals(botMessageResponse.getMessageType())
            && BotUtil.isGroupMember(botMessageResponse.getGroupId(), 1685410989L)) {
          BotUtil.sendMessage(botMessageResponse, "[CQ:at,qq=1685410989] 太呆啦");
        }
      }
      case "阳哥儿" -> {
        if (MessageTypeEnum.GROUP.getType().equals(botMessageResponse.getMessageType())
            && BotUtil.isGroupMember(botMessageResponse.getGroupId(), 1121138404L)) {
          BotUtil.sendMessage(botMessageResponse, "[CQ:at,qq=1121138404] 太呆啦");
        }
      }
      case "早安" -> BotUtil.sendMessage(botMessageResponse, apiUtil.goodMorning());
      case "午安" -> BotUtil.sendMessage(botMessageResponse, apiUtil.goodAfternoon());
      case "晚安" -> BotUtil.sendMessage(botMessageResponse, apiUtil.goodNight());
      case "励志" -> BotUtil.sendMessage(botMessageResponse, apiUtil.motivational());
      case "午饭" -> this.onLunchOrder(botMessageResponse);
      case "午饭历史" -> this.onLunchHistoryOrder(botMessageResponse);
      case "帮助" -> BotUtil.sendReadme(botMessageResponse);
      case "提醒微博签到" -> this.remindWeiboCheckin(botMessageResponse);
    }
    if (order.contains("待办")) {
      iTodoService.onOrder(order, botMessageResponse);
    }
    if (order.startsWith("二维码")) {
      iPicService.getOrderQRCode(botMessageResponse, order.substring(3));
    }
  }

  @Override
  public void onLunchOrder(BotMessageResponse botMessageResponse) {
    String todayLunch = iLunchInfoService.today(String.valueOf(botMessageResponse.getUserId()));
    String message =
        apiUtil.goodAfternoon()
            + "\n"
            + BotUtil.getCQAtStr(botMessageResponse.getUserId())
            + "小主，奴婢今日为您随机的午饭是"
            + todayLunch;
    if (Constants.QR_CODE_LIST.contains(todayLunch)) {
      message += "\n" + BotUtil.getCQImgStr(todayLunch + ".jpg");
    }
    BotUtil.sendMessage(botMessageResponse, message);
  }

  @Override
  public void onLunchHistoryOrder(BotMessageResponse botMessageResponse) {
    String resStr = iLunchInfoService.lunchListInfo(String.valueOf(botMessageResponse.getUserId()));
    BotUtil.sendMessage(
        botMessageResponse, botMessageResponse.getSender().getNickname() + "\n" + resStr);
  }

  @Override
  public void remindWeiboCheckin(BotMessageResponse botMessageResponse) throws SchedulerException {
    if (iBotService.isNotMaster(botMessageResponse.getUserId())) {
      return;
    }
    quartzUtil.addQuartzJob(
        RemindWeiboCheckinJob.class,
        new TriggerKey("RemindWeiboCheckinJob", "Checkin"),
        TriggerBuilder.newTrigger()
            .startNow()
            .withSchedule(SimpleScheduleBuilder.repeatHourlyForTotalCount(3, 24)),
        new JobDataMap(Map.of(Constants.GROUP_ID_JOB_DATA_KEY, botMessageResponse.getGroupId())),
        "提醒三天微博签到",
        true);
  }
}
